import config from 'config'
import httpFetch from 'share/httpFetch'
import errorMessage from 'share/errorMessage';
import { messages } from "share/common";
import moment from 'moment'

function createPieOptions(data, config) {
  let { title } = config;
  let options = {
    title: {
      text: title,
      subtext: data,
      left: 'center',
      top: '45%',   // 这边居中实在不好弄
      bottom: 'center',
      textVerticalAlign: 'middle',
      padding: 0,
      itemGap: 5,
      textStyle: {
        fontWeight: 400,
        fontSize: 16,
        color: '#6C788C',
        height: 16
      },
      subtextStyle: {
        fontWeight: 400,
        fontSize: 26,
        color: '#4E5B71',
        height: 24
      }
    },
    tooltip: {
      show: false
    },
    legend: {
      show: false
    },
    grid: {
      left: '1%',
      right: '1%',
      bottom: '0%',
      top: '1%',
      containLabel: true
    },
    series: [
      {
        type: 'pie',
        radius: ['55%', '65%'],
        selectedOffset: 5,
        label: {
          show: false
        },
        labelLine: {
          show: false
        },
        data: [
          {
            value: 0,
            selected: true,
            emphasis: {
              itemStyle: {
                color: '#1890FF'
              }
            }
          },
        ]
      },
    ],
    color: [
      '#1890FF',
      '#F1F2F5'
    ]
  };
  let dataNum = parseFloat(data) || 0;
  options.series[0].data[0].value = dataNum;
  if (dataNum < 100) {
    options.series[0].data.push({
      value: 100 - dataNum,
      emphasis: { // 不加这个样式hover的时候会变白，不知道为什么
        itemStyle: {
          color: '#F1F2F5'
        }
      }
    })
  }
  return options
}

function createBarOptions(data) {
  const colorList = ['#6CADEC', '#64A3EE', '#5A97F0', '#538FF3', '#4A82F4', '#4379F6'];
  let monthList = [];
  let valueList = [];
  if (typeof data === 'object' && data) {
    Object.keys(data).forEach((key, index) => {
      let month = moment(key).format('MMM');
      monthList.push(month);
      valueList.push({
        value: data[key],
        itemStyle: {
          color: colorList[index]
        }
      })
    });
  }

  return {
    tooltip: {
      trigger: 'axis',
      axisPointer: {            // 坐标轴指示器，坐标轴触发有效
        type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
      }
    },
    grid: {
      left: '5%',
      right: '5%',
      bottom: '9%',
      top: '1%',
      containLabel: true
    },
    xAxis: [
      {
        data: monthList,
        axisLine: {
          lineStyle: {
            color: '#DBE6F3'
          }
        },
        axisTick: {
          show: false
        },
        axisLabel: {
          color: '#6C788C',
          fontSize: 12
        },
      }
    ],
    yAxis: [
      {
        type: 'value',
        boundaryGap: ['0', '10%'],  // 顶部留白
        splitLine: {
          lineStyle: {
            type: 'dashed',
            color: '#EBF0F5'
          }
        },
        axisLine: {
          show: false
        },
        axisTick: {
          show: false
        },
        axisLabel: {
          show: false
        },
      }
    ],
    series: [
      {
        type: 'bar',
        barWidth: 20,
        data: valueList,
      }
    ]
  }
}

function createLineOptions(data) {
  let monthList = [];
  let valueList = [];
  if (typeof data === 'object' && data) {
    Object.keys(data).forEach((key, index) => {
      let month = moment(key).format('MMM');
      monthList.push(month);
      valueList.push(data[key])
    });
  }

  return {
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      show: false
    },
    grid: {
      left: '5%',
      right: '5%',
      bottom: '9%',
      top: '1%',
      containLabel: true
    },
    xAxis: {
      data: monthList,
      axisLine: {
        lineStyle: {
          color: '#DBE6F3'
        }
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        color: '#6C788C',
        fontSize: 12
      },
    },
    yAxis: {
      type: 'value',
      boundaryGap: ['0', '10%'],  // 顶部留白
      splitLine: {
        lineStyle: {
          type: 'dashed',
          color: '#EBF0F5'
        }
      },
      axisLine: {
        show: false
      },
      axisTick: {
        show: false
      },
      axisLabel: {
        show: false
      },
    },
    series: [
      {
        type: 'line',
        step: 'end',
        data: valueList
      },
    ],
    color: '#4DC690'
  }
}

export default {
  //获取卡片上的数据
  getData: function () {
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/home/page/statistics/qty')
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  getHomeBodyData(yearMonthFilter) {
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/home/page/statistics/body/qty', {
        yearMonthFilter
      })
        .then((res) => {
          resolve(res)
        })
        .catch((err) => {
          errorMessage(err.response);
          reject(err);
        })
    })
  },

  createOptions({ data, type, config }) {
    const typeMap = {
      'pie': createPieOptions,
      'bar': createBarOptions,
      'line': createLineOptions
    };

    let creator = typeMap[type];
    if (creator) {
      return creator(data, config);
    }
  }
}
